﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrumTime.Models;
using System.Web.Security;

namespace ScrumTime.ViewModels
{
    public class UserViewModel
    {

        private List<UserInfo> _users;
        private List<UserInfo> _usersNotInProduct;

        private ScrumTimeEntities _ScrumTimeEntities;
        private bool _isAdmin;
        private bool _cantEditUser;

        private int productId;

        public UserViewModel( int p , bool isAdmin, bool cantEditUser) {
            _users = new List<UserInfo>();
            _usersNotInProduct = new List<UserInfo>();
            _ScrumTimeEntities = new ScrumTimeEntities();            
            productId = p;
            FillUsers();
            this._isAdmin = isAdmin;
            this._cantEditUser = cantEditUser;
        }

        public List<ScrumTime.Models.Role> GetRoles(){
            return _ScrumTimeEntities.Roles.ToList();
        }


        public bool IsAdmin() {
            return this._isAdmin;
        }

        public bool CantEdit()
        {
            return this._cantEditUser;
        }

        private void FillUsers() {

            List<String> guiUserProduct = this.getGUIUserProducts();

            AccountMembershipService membershipService = new AccountMembershipService();
            MembershipUserCollection membershipUserCollection = membershipService.GetAllUsers();
            foreach (MembershipUser user in membershipUserCollection)
            {

                String user_guid = user.ProviderUserKey.ToString();

                var results = from t in _ScrumTimeEntities.Users
                              where t.UserName == user.UserName
                              select t;

                if (results.Count() > 0) {
                    if (guiUserProduct.Contains(user.ProviderUserKey.ToString()))
                    {
                        _users.Add(new UserInfo(null, user));
                    }
                    else
                    {
                        _usersNotInProduct.Add(new UserInfo(null, user));
                    }
                }
            }
        }

        private Product getProduct() {
            Product resultado = null;
            foreach (Product p in _ScrumTimeEntities.Products) {
                if (p.ProductId == productId) {
                    resultado = p;
                    break;
                }
            }
            return resultado;
        }

        private List<String> getGUIUserProducts() {            
            ScrumTime.Services.UserService userService = new ScrumTime.Services.UserService(this._ScrumTimeEntities);
            Product prod = getProduct();
            return userService.GetGUIUserProducts(prod);

        }

        public List<UserInfo> Users {
            get {                
                return this._users;
            }
        }

        public List<UserInfo> UserNotInProduct {
            get
            {                
                return this._usersNotInProduct;
            }
        }


    }
}